<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<title>Semaphores</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="generator" content="Doc-O-Matic" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <link rel="STYLESHEET" href="default.css" type="text/css" />

<script type="text/javascript" src="scripts.js"></script>
</head>
<body class="Element700" onload="onBodyLoadEx('frames.html', 'topic', '00644.html');" onmousedown="onBodyMouseDown();">

<!-- Begin Popups -->

<!-- End Popups -->

<!-- Begin Page Header -->
<div class="Element710" id="areafixed">
<div class="Element94">
<a href="00626.html" target="topic">OSAL Library Help</a> &gt; <a href="00645.html" target="topic">Using the Library</a> &gt; <a href="00633.html" target="topic">How the Library Works</a> &gt; <a href="00644.html" target="topic">Semaphores</a></div>
<div class="Element92">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="25%">
<div class="Element1">
MPLAB Harmony Core Help</div>
</td><td width="25%">
<div class="Element2">
<a href="contents.html" target="tocidx">Contents</a> | <a href="00007.html" target="topic">Home</a></div>
</td><td width="25%">
<div class="Element90">
<a href="00633.html" target="topic">Previous</a> | <a href="00633.html" target="topic">Up</a> | <a href="00640.html" target="topic">Next</a></div>
</td><td width="25%">
<div class="Element96">
<a href="mailto:docerrors@microchip.com&subject=MPLAB Harmony Documentation Feedback (Topic ID: OSAL Semaphores Topic Title: Semaphores)&body=Thank you for your feedback! Please include a description of your feedback, and indicate whether you are reporting an an error in the documentation or an enhancement.">Documentation Feedback</a><br> <a href="http://support.microchip.com" target="_blank">Microchip Support</a></div>
</td></tr></table><div class="Element5">
Semaphores</div>
</div>
</div>

<!-- End Page Header -->

<!-- Begin Client Area -->
<div class="Element720" id="areascroll">
<div class="Element721">

<!-- Begin Page Content -->
<a name="PageContent"></a><div class="Element58">
<a name="4465736372697074696F6E"></a><div class="Element11">
<div class="Element10">
<p class="Element10" style="text-align: justify;">
A semaphore can be used to lock a shared resource, although it is more normal to use a mutex for such an activity. Once obtained a semaphore should be posted back to enable it to be retaken at a later time or in another thread.</p><div class="Element13"><div class="Element12"><pre class="Element12"><i><span style="color: #008000">/* mainline code prior to OS start */</span></i>
    <i><span style="color: #008000">/* declare a variable of type semaphore handle */</span></i>
    OSAL_SEM_DECLARE(semSync);
    <i><span style="color: #008000">/* create the semaphore */</span></i>
    OSAL_SEM_Create(&amp;semSync, OSAL_SEM_TYPE_BINARY, 0, 0);


<i><span style="color: #008000">/* thread one */</span></i>
    ...
    <i><span style="color: #008000">/* take the semaphore without waiting */</span></i>
    OSAL_SEM_Pend(semSync, 0);
    ... perform some actions
    <i><span style="color: #008000">/* return the semaphore */</span></i>
    OSAL_SEM_Post(semSync);
    ...

<i><span style="color: #008000">/* thread two must not execute until thread one has finished its operations*/</span></i>
    ...
    <i><span style="color: #008000">/* block on the semaphore */</span></i>
    OSAL_SEM_Pend(semSync, OSAL_WAIT_FOREVER);
    ... perform some more actions
    <i><span style="color: #008000">/* return the semaphore */</span></i>
    OSAL_SEM_Post(semSync);</pre></div></div>
<p class="Element10" style="text-align: justify;">
&nbsp;</p>
<p class="Element10" style="text-align: justify;">
A semaphore can be signalled multiple times and so provides a method for an ISR to release a thread waiting on it. Even though the blocked thread never returns the semaphore, because the asynchronous ISR repeatedly posts it the next time the thread wants to pend on the semaphore it will be available. By moving the majority of interrupt service processing from the ISR to a high priority thread the system response time is improved and the eventual processing can take advantage of OSAL features such as mutexes and queues which would normally be harder to implement inside the ISR. This technique is known as deferred interrupt processing.&nbsp;</p>
<p class="Element10" style="text-align: justify;">
&nbsp;</p>
<p class="Element10" style="text-align: justify;">
&nbsp;</p><div class="Element13"><div class="Element12"><pre class="Element12"><i><span style="color: #008000">/* an example interrupt handler called from an ISR that performs task synchronization using a semaphore */</span></i>
<strong><span style="color: #000080">void</span></strong> _ISRTasksRX(<strong><span style="color: #000080">void</span></strong>) <i><span style="color: #008000">/* N.B. pseudo-code ISR */</span></i>
{
    ...

    _DRV_USART_InterruptSourceStatusClear(_DRV_USART_GET_INT_SRC_RX(_DRV_USART_OBJ(dObj, rxInterruptSource)));

    <i><span style="color: #008000">/* Release the receive semaphore unblocking any tasks */</span></i>
    OSAL_SEM_PostISR(_DRV_USART_OBJ(dObj, rxSemID));

} <i><span style="color: #008000">/* DRV_USART_TasksRX */</span></i></pre></div></div>
</div>
</div>
</div>
<!-- End Page Content -->

<!-- Begin Page Footer -->
<div class="Element95">
<a href="00626.html" target="topic">OSAL Library Help</a> &gt; <a href="00645.html" target="topic">Using the Library</a> &gt; <a href="00633.html" target="topic">How the Library Works</a> &gt; <a href="00644.html" target="topic">Semaphores</a></div>
<div class="Element93">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="25%">
<div class="Element3">
MPLAB Harmony Core Help</div>
</td><td width="25%">
<div class="Element4">
<a href="contents.html" target="tocidx">Contents</a> | <a href="00007.html" target="topic">Home</a></div>
</td><td width="25%">
<div class="Element91">
<a href="00633.html" target="topic">Previous</a> | <a href="00633.html" target="topic">Up</a> | <a href="00640.html" target="topic">Next</a></div>
</td><td width="25%">
<div class="Element97">
<a href="mailto:docerrors@microchip.com&subject=MPLAB Harmony Documentation Feedback (Topic ID: OSAL Semaphores Topic Title: Semaphores)&body=Thank you for your feedback! Please include a description of your feedback, and indicate whether you are reporting an an error in the documentation or an enhancement.">Documentation Feedback</a><br> <a href="http://support.microchip.com" target="_blank">Microchip Support</a></div>
</td></tr></table></div>

<!-- End Page Footer -->
</div>
</div>

<!-- End Client Area -->
</body></html>